package com.tulele.vtool.handler;

import com.google.common.collect.Maps;
import com.tulele.vtool.model.UserImpressionInfo;
import com.tulele.vtool.model.combinedEntity.CombinedUserCurImpressionInfo;
import com.tulele.vtool.model.reqEntity.AddImpressionReqBean;
import com.tulele.vtool.model.reqEntity.AgreeUserImpressionReqBean;
import com.tulele.vtool.model.reqEntity.GetUserCurImpressionReqBean;
import com.tulele.vtool.model.reqEntity.RemoveUserImpressionInfoReqBean;
import com.tulele.vtool.model.respEntity.RespData;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.service.IImpressionInfoService;
import com.tulele.vtool.service.IUserImpressionInfoService;
import com.tulele.vtool.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
public class ImpressionInfoHandler {

    @Autowired
    private IImpressionInfoService impressionInfoService;

    @Autowired
    private IUserImpressionInfoService userImpressionInfoService;

    @PostMapping(value = "/getDefaultImpression", produces="application/json;charset=UTF-8")
    public RespData getDefaultImpression() {

        List<String> impressionName = impressionInfoService.getPlatformImpressionName();

        HashMap<String, String> resultMap = Maps.newHashMapWithExpectedSize(2);
        resultMap.put("impressionName", String.join(",", impressionName));

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(resultMap);
        respData.setMsg("成功");
        return respData;
    }

    @PostMapping(value = "/getUserCurImpression", produces="application/json;charset=UTF-8")
    public RespData getUserCurImpression(@RequestHeader("userId") String userId, @RequestBody GetUserCurImpressionReqBean getUserCurImpressionReqBean) {

        Integer userIdInt = Integer.valueOf(userId);

        if (getUserCurImpressionReqBean != null) {

            String otherUserId = getUserCurImpressionReqBean.getOtherUserId();
            if (StringUtils.isNotBlank(otherUserId)) {
                userIdInt = Integer.valueOf(otherUserId);
            }

        }

        List<CombinedUserCurImpressionInfo> userImpressionInfoList = userImpressionInfoService.getUserCurImpression(userIdInt);

        HashMap<String, Object> resultMap = Maps.newHashMapWithExpectedSize(2);
        resultMap.put("curImpressionInfo", userImpressionInfoList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(resultMap);
        respData.setMsg("成功");
        return respData;
    }

    @PostMapping(value = "/addImpression", produces="application/json;charset=UTF-8")
    public RespData addImpression(@RequestHeader("userId") String userId, @RequestBody AddImpressionReqBean addImpressionReqBean) {

        Integer userIdInt = Integer.valueOf(userId);

        if (addImpressionReqBean == null || StringUtils.isBlank(addImpressionReqBean.getImpressionName())) {

            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;

        } else {

            String impressionName = addImpressionReqBean.getImpressionName().trim();

            String otherUserId = addImpressionReqBean.getOtherUserId();
            if (StringUtils.isNotBlank(otherUserId)) {
                userIdInt = Integer.valueOf(otherUserId);
            }

            userImpressionInfoService.addUserImpression(userIdInt, impressionName);
        }

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;

    }

    @PostMapping(value = "/agreeUserImpression", produces="application/json;charset=UTF-8")
    public RespData agreeUserImpression(@RequestHeader("userId") String userId, @RequestBody AgreeUserImpressionReqBean agreeUserImpressionReqBean) {

        if (agreeUserImpressionReqBean == null || agreeUserImpressionReqBean.getImpressionId() == null) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        userImpressionInfoService.agreeUserImpression(agreeUserImpressionReqBean.getImpressionId());

        //查询点赞次数
        Integer impressionCount = userImpressionInfoService.getUserImpressionAgreeCount(agreeUserImpressionReqBean.getImpressionId());

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);

        Map<String, Integer> resultMap = Maps.newHashMapWithExpectedSize(2);
        resultMap.put("impressionCount", Optional.ofNullable(impressionCount).orElse(0));

        respData.setData(resultMap);
        respData.setMsg("成功");
        return respData;
    }

    @PostMapping(value = "/removeUserImpressionInfo", produces="application/json;charset=UTF-8")
    public RespData removeUserImpressionInfo(@RequestHeader("userId") String userId, @RequestBody RemoveUserImpressionInfoReqBean removeUserImpressionInfoReqBean) {

        if (removeUserImpressionInfoReqBean == null || removeUserImpressionInfoReqBean.getImpressionId() == null) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        UserImpressionInfo userImpressionInfo = new UserImpressionInfo();
        userImpressionInfo.setId(removeUserImpressionInfoReqBean.getImpressionId());
        userImpressionInfo.setImpressionStatus(0);
        userImpressionInfo.setImpressionCount(0);
        userImpressionInfo.setModifyTime(DateUtil.getCurDate());
        userImpressionInfoService.modifyUserImpressionInfo(userImpressionInfo);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }


}
